package hxy.scan.server.controller;

import hxy.scan.server.dao.mapper.CustomerMapper;
import hxy.scan.server.dao.model.CustomerModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
public class CustomerController {


    @Resource
    CustomerMapper customerMapper;

    @Resource
    AmqpTemplate amqpTemplate;


    @Resource
    RedisTemplate redisTemplate;


    @GetMapping("/customer-create")
    public int create(CustomerModel customerEntity) {

        String name = customerEntity.getName();

        if (name == null) {
            return 0;
        }

//        amqpTemplate.convertAndSend("cupb", customerEntity);

        return customerMapper.insert(customerEntity);
    }

    @GetMapping("/customer-redis-set")
    public String redisCreate(CustomerModel customerEntity) {

        ValueOperations valueOperations = redisTemplate.opsForValue();

        valueOperations.set(customerEntity.getName(), customerEntity);

        valueOperations.set(customerEntity.getName(), customerEntity, 1, TimeUnit.HOURS);

        return "成功";
    }

    @GetMapping("/customer-redis-get")
    public Object redisGet(String name) {
        Object o = redisTemplate.opsForValue().get(name);
        return o;
    }


    @GetMapping("/customer-list")
    public List<CustomerModel> list(String 张三) {

        List<CustomerModel> o = (List<CustomerModel>) redisTemplate.opsForValue().get(张三);
        if (o != null) {
            return o;
        }

        List<CustomerModel> customerModels = customerMapper.selectList(null);

        ArrayList<CustomerModel> arrayList = new ArrayList<>();
        for (CustomerModel customerModel : customerModels) {
            String name = customerModel.getName();
            log.info(name);


            if (name.equals(张三)) {
                log.info("有问题");
                arrayList.add(customerModel);
            }

        }

        redisTemplate.opsForValue().set(张三, arrayList);


        return arrayList;
    }

    @GetMapping("/customer-delete")
    public int delete(int name) {
        return customerMapper.deleteById(name);
    }


    @GetMapping("/customer-update")
    public int update(CustomerModel customerEntity) {

        return customerMapper.updateById(customerEntity);

    }

    public static void main(String[] args) {
        ArrayList<String> sites = new ArrayList<String>();
        sites.add("Google");
        sites.add("Runoob");
        sites.add("Taobao");
        sites.add("Weibo");
        System.out.println(sites);
    }


}



